import java.io.PrintStream;
import java.util.Random;
import java.util.ArrayList;
import utils.*; 
public class BoySphere {
	private static double max(double x,double y){
		if(x>y) return x; 
		else return y; 
	}
	public static void main(String[] args) throws Exception{
		PrintStream output=new PrintStream("/tmp/outBOY.txt");
		ArrayList<Sphere> allSpheres=new ArrayList<Sphere>(); 
		Complexe z,zaux; 
		Random gene=new Random();
		Complexe UN=Complexe.createComplexeFromRI(1, 0);  
		output.println("#declare boy=union{"); 
		for(int i=0;i<300000;i++){
		
			double yz=2*gene.nextDouble()-1;
			double xz=2*gene.nextDouble()-1; 
			
			//if(gene.nextDouble()>0.5)xz=-xz; 
			if((xz*xz+yz*yz)<=1){
			z=Complexe.createComplexeFromRI(xz, yz);
			//System.out.println(xz+" "+yz); 
			//if(yz>0.706) {System.out.println("fini"); System.exit(0);} 
			
			Complexe z6=Complexe.intPower(z, 6); 
			Complexe z3=Complexe.intPower(z, 3);
			Complexe z4=Complexe.intPower(z, 4);
			
			Complexe numg=Complexe.mulbyDouble(z3, Math.sqrt(5));
			numg=Complexe.sum(numg, z6); 
			numg=Complexe.sub(numg,UN);
			
			// g1
			Complexe ag1=Complexe.sub(UN,z4); 
			Complexe bg1=Complexe.mul(z,ag1); 
			Complexe zg1=Complexe.div(bg1, numg); 
			double g1=-3/2*zg1.getIm(); 
			
			// g2
			Complexe ag2=Complexe.sum(UN,z4); 
			Complexe bg2=Complexe.mul(z,ag2); 
			Complexe zg2=Complexe.div(bg2, numg); 
			double g2=-3/2*zg2.getReal();
			
			// g3
			Complexe ag3=Complexe.sum(UN,z6); 
			Complexe bg3=Complexe.div(ag3,numg); 
			double g3=bg3.getIm()-0.5; 
			
			double g=(g1*g1)+(g2*g2)+(g3*g3); 
			
			Sphere sp=new Sphere(g1/g,g2/g,g3/g,0.1);
			double minradius=0.1;
			double radius=minradius; 
			for(Sphere spc:allSpheres){
				if(sp.intersects(spc)) {
						radius=spc.distance(sp)-spc.getRadius();
						//if(radius<0){System.out.println(radius); System.exit(0); }
						if(radius<minradius)minradius=radius; 
					
				}
				sp.setRadius(minradius); 
			
			}
			if(sp.getRadius()>0.00001)
			allSpheres.add(sp);		
			}
			if(i%1000==0) System.out.println(i);
		}
		for(Sphere sp:allSpheres) {
			output.println(sp);
			//System.out.println(sp); 
		}
		System.out.println(allSpheres.size()); 
		output.println("}");
		System.out.println("done"); 
		
		
	}
	
	

}
